package com.ruoyi.message.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.message.domain.GroupUserId;
import com.ruoyi.message.service.IGroupMessageService;
import com.ruoyi.message.service.IGroupUserIdService;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.message.domain.GroupUser;
import com.ruoyi.message.service.IGroupUserService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 群Controller
 *
 * @author ruoyi
 * @date 2023-10-19
 */
@RestController
@RequestMapping("/group")
public class GroupUserController extends BaseController {
    @Autowired
    private IGroupUserService groupUserService;
    @Autowired
    private IGroupUserIdService userIdService;
    @Autowired
    private IGroupMessageService messageServicel;

    /**
     * 查询群列表
     */
    @RequiresPermissions("message:group:list")
    @GetMapping("/list")
    public TableDataInfo list(GroupUser groupUser) {
        startPage();
        List<GroupUser> list = groupUserService.selectGroupUserList(groupUser);
        return getDataTable(list);
    }
    @PostMapping("/lists/{id}")
    public TableDataInfo lists(@PathVariable("id") String id) {
        GroupUserId groupUserId = new GroupUserId();
        groupUserId.setUserId(Long.valueOf(id));
        List<GroupUserId> list =  userIdService.selectGroupUserIdListByuserId(groupUserId);
        List<GroupUser> list1=new ArrayList<>();
        for (GroupUserId userId : list) {
            GroupUser groupUser = groupUserService.selectGroupUserById(userId.getGroupId());
            list1.add(groupUser);
        }
        return getDataTable(list1);
    }

    /**
     * 导出群列表
     */
    @RequiresPermissions("message:group:export")
    @Log(title = "群", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GroupUser groupUser) {
        List<GroupUser> list = groupUserService.selectGroupUserList(groupUser);
        ExcelUtil<GroupUser> util = new ExcelUtil<GroupUser>(GroupUser.class);
        util.exportExcel(response, list, "群数据");
    }

    /**
     * 获取群详细信息
     */
    @RequiresPermissions("message:group:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(groupUserService.selectGroupUserById(id));
    }

    /**
     * 新增群
     */
    @RequiresPermissions("message:group:add")
    @Log(title = "群", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GroupUser groupUser) {
        //设置群主
        LoginUser loginUser = SecurityUtils.getLoginUser();
        groupUser.setCreateBy(loginUser.getSysUser().getNickName());
        groupUser.setCreateId(loginUser.getUserid());
        //添加群主
        int i = groupUserService.insertGroupUser(groupUser);
        GroupUserId groupUserId = new GroupUserId();
        groupUserId.setGroupId(groupUser.getId());
        groupUserId.setUserId(loginUser.getUserid());
        userIdService.insertGroupUserId(groupUserId);
        return toAjax(i);
    }

    /**
     * 修改群
     */
    @RequiresPermissions("message:group:edit")
    @Log(title = "群", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GroupUser groupUser) {
        return toAjax(groupUserService.updateGroupUser(groupUser));
    }

    /**
     * 删除群
     */
    @RequiresPermissions("message:group:remove")
    @Log(title = "群", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        userIdService.deleteGroupUserIdByGroupIds(ids);
        messageServicel.deleteGroupMessageByIds(ids);
        return toAjax(groupUserService.deleteGroupUserByIds(ids));
    }
}
